package com.org.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.org.bean.PhoneBean;
import com.org.service.IPhoneService;
import com.org.utils.MyUtils;

@Controller
public class ProductAction {

		// 依赖注入【反转控制】
		// 面向接口的编程
		@Resource
		private IPhoneService proS;// 按名称匹配进行注入
		
		
		/**
		 * 页面一加载即查询所有未删除商品的信息
		 * @return 查询到的信息
		 */
		@ResponseBody
		@RequestMapping(value = "AjaxQueryPhoneAction", method = RequestMethod.GET)
		public List<PhoneBean> ajaxQueryDemo() {
			// 调用service层进行查询操作【查询所有信息】
			List<PhoneBean> lstPros = proS.selectAllInfos();
			// 直接返回查询结果集即可，springmvc框架自动将结果集转换为json字符串返回给客户端
			System.out.println(JSONObject.toJSONString(lstPros));
			return lstPros;
		}
		
		
		
		/**
		 * springmvc的ajax查询
		 * 
		 * @param phoneB
		 * @return
		 */
		@ResponseBody
		@RequestMapping("AjaxQueryProAction")
		public List<PhoneBean> ajaxQueryDemo(PhoneBean phoneB) {
			// 调用service层进行查询操作
			List<PhoneBean> lstPros = proS.queryInfoByParams(phoneB);
			// 直接返回查询结果集即可，springmvc框架自动将结果集转换为json字符串返回给客户端
			return lstPros;
		}
		
		
		/**
		 * 文件上传处理，MultipartFile file_phoneimg变量用于接收form表单提交的字节流对象，通过该对象在后台进行文件保存处理即可
		 * 
		 * @param file_phoneimg
		 * @param phoneB
		 * @return
		 * @throws IOException 
		 */
		@RequestMapping("/PhoneAddWithFile")
		public void phoneAddWithFile(HttpServletRequest request, PhoneBean phoneB,HttpServletResponse response) throws IOException {

			// 调用utils的方法，进行文件上传处理
			MyUtils.uploadFilesWithRequest(request, phoneB, "phoneImgs", "phoneImg");

			// 进行文件保存处理
			int i = proS.saveOrUpdatePhone(phoneB);
			// 调用service层的业务逻辑处理
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out=response.getWriter();
			
			// 根据service层操作结果进行跳转处理
			if (i > 0) {
				// 新增成功
				out.write("<script>var isAdd=window.confirm('新增成功！是否继续增加？');if(isAdd){window.location.href='jump?pg=phoneInfoManager/phoneAdd'; }else{window.location.href='jump?pg=welcome';}</script>");		
			} else {
				// 新增失败
				out.write("<script>alert('新增修改失败！');window.location.href='jump?pg=welcome';</script>");		
			}
		}
		
		
		/**
		 * 删除
		 * @param phoneId
		 * @return
		 */
		@RequestMapping(value = "/phoneInfo/{phoneId}", method = RequestMethod.DELETE)
		public ResponseEntity<List<PhoneBean>> restDeletephoneInfo_DeleteById(
				@PathVariable("phoneId") Integer phoneId) {
			// 调用service层进行逻辑删除
			int i = proS.logicDeleteByPrimaryKey(phoneId);
			// 根据删除操作结果进行转发处理
			// 调用service层进行查询操作
			// 需要进行修订，查询条件应该是删除时的查询的条件
			// TODO 待优化
			List<PhoneBean> lstPros = proS.queryInfoByParams(new PhoneBean());
			// 直接返回查询结果集即可
			return ResponseEntity.status(HttpStatus.OK).body(lstPros);
		}
		
		
		
		@RequestMapping(value = "/phoneInfo/{phoneId}", method = RequestMethod.POST)
		public ResponseEntity<Void> restUpdatePhoneInfo_PutById(HttpServletRequest request,
				@PathVariable("phoneId") Integer phoneId, PhoneBean phoneB) {
			
			MyUtils.uploadFilesWithRequest(request, phoneB, "phoneImgs", "phoneImg");

			System.out.println(phoneB.toString()+"(*^▽^*)");
			
			// 调用service层进行逻辑修改
			int i = proS.updateByPrimaryKeySelective(phoneB);
			
			// 直接返回查询结果集即可
			return new ResponseEntity<Void>(HttpStatus.OK);
		}
		
		
		@RequestMapping(value = "/phoneInfo/{phoneId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
		public ResponseEntity<List<PhoneBean>> restQueryPhoneInfo_GetId(@PathVariable("phoneId") Integer phoneId) {
			// 调用service层进行查询操作
			List<PhoneBean> lstPros = proS.selectByPrimaryKeyList(phoneId);
			// 直接返回查询结果集即可，springmvc框架自动将结果集转换为json字符串返回给客户端
			return ResponseEntity.status(HttpStatus.OK).body(lstPros);
		}
		
		
}
